<?php
/**
 * Clase Utils
 * @author chalodrv@yahoo.es
 */
class Utils
{
	/**
	 * Variables
	 */

	/**
	 * Constructor
	 */
	function __construct()
	{
	}

	/**
	 * Método checkProtocol
	 * @param string $section
	 */
	function checkProtocol($section)
	{
		$secureSections = explode(",", SECURE_SECTIONS);

		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
			if (!in_array($section, $secureSections)) {
				header("Location: ".BASE_URL.$_SERVER["REQUEST_URI"]);
				exit;
			}
		} else {
			if (in_array($section, $secureSections)) {
				$url = SECURE_URL.$_SERVER["REQUEST_URI"];
				var_dump($url);exit;
				header("Location: ".$url);
				exit;
			}
		}
	}

	/**
	 * Método generateSEOUrl
	 * @param string $language
	 * @param string $section
	 * @param string $subsection
	 * @param string $page
	 */
	function generateSEOUrl($language=DEFAULT_LANGUAGE, $section=DEFAULT_SECTION, $subsection="", $page="")
	{
		$ssl = in_array($section, explode(",", SECURE_SECTIONS)) ? true : false;
		$url = $ssl ? SECURE_URL : BASE_URL;
		$url .= $language."/";
		$url .= $section."/";
		if (!empty($subsection)) $url .= $subsection."/";
		if (!empty($page)) $url .= $page."/";
		return $url;
	}

	/**
	 * Método getBaseUrl
	 * @param string $language
	 * @param string $section
	 * @param string $subsection
	 */
	function getBaseUrl($language=DEFAULT_LANGUAGE, $section=DEFAULT_SECTION, $subsection="")
	{
		$url = BASE_URL.$language."/".$section."/";
		if (strlen($subsection)) {
			$url .= $subsection."/";
		}
		return $url;
	}

	/**
	 * Método generateRandomCode
	 */
	function generateRandomCode()
	{
		return substr(md5(microtime()), 0, 20);
	}

	/**
	 * Método writeLog
	 * @param string $filename
	 * @param array $data
	 */
	function writeLog($filename, $data)
	{
		if (!empty($filename)) {
			$f = fopen(LOGS_DIR.$filename.".log", "a");
			fwrite($f, date("Y-m-d H:i:s")." :: ".implode(" :: ", $data)."\n");
			fclose($f);
		}
	}
	
	/**
	 * Método setLanguage
	 */
	function setLanguage()
	{
		$language = DEFAULT_LANGUAGE;
		if (!empty($_GET["language"]) && in_array($_GET["language"], unserialize(AVAILABLE_LANGUAGES))) {
			$language = $_GET["language"];
		}
		return $language;
	}

	/**
	 * Método redirect
	 * @param string $url
	 */
	function redirect($url)
	{
		// Redirigimos a la url especificada
		header("location: ".$url);
		exit;
	}
	
	/**
	 * Método toUpper
	 * @param string $string
	 */
	function toUpper($string)
	{
		$string = strtoupper($string);
		$string = strtr($string, "áéíóúñäëïöü", "ÁÉÍÓÚÑÄËÏÖÜ");
		return $string;
	}
}
?>
